package com.zsr.web.controller.common;

import com.aliyun.sts20150401.models.AssumeRoleResponseBody;
import com.zsr.common.core.domain.AjaxResult;
import com.zsr.common.utils.oss.AliCloudUtil;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/oss")
public class OssController {
    @Resource
    AliCloudUtil aliCloudUtil;

    @PostMapping("/upload")
    public AjaxResult uploadFile(MultipartFile file) {
        //返回上传oss的url
        String url = aliCloudUtil.uploadOneFile(file, null);
        AjaxResult ajax = AjaxResult.success();
        ajax.put("fileName", file.getOriginalFilename());
        ajax.put("url", url);
        return ajax;
    }

    @PostMapping("/upload/fixed")
    public AjaxResult uploadFileFixedName(MultipartFile file, @RequestParam String fileName) {
        // 返回上传oss的url
        String url = aliCloudUtil.uploadOneFileFixedName(file, fileName);
        AjaxResult ajax = AjaxResult.success();
        ajax.put("fileName", fileName);
        ajax.put("url", url);
        return ajax;
    }

    @PostMapping("/uploadArrayFile")
    public List<String> uploadArrayFile(MultipartFile[] files) {
        //返回上传oss的url
        return aliCloudUtil.uploadArrayFile(files);
    }

    @PostMapping("/deleteFile")
    public boolean deleteFile(@RequestBody String fileUrl) {
        //返回是否删除成功
        return aliCloudUtil.deleteFile(fileUrl);
    }

    @GetMapping("/getStsToken")
    public AjaxResult getStsToken() {
        AssumeRoleResponseBody.AssumeRoleResponseBodyCredentials stsToken = aliCloudUtil.getStsToken();
        if (stsToken == null) {
            return AjaxResult.error("stsToken获取失败");
        }
        return AjaxResult.success(stsToken);
    }
}
